%%%-------------------------------------------------------------------
%%% @author liuwentao
%%% @doc
%%%
%%% @end
%%% Created : 24. 5月 2021 14:14
%%%-------------------------------------------------------------------
-module(logger).
-author("liuwentao").

%% API
-export([
    info/4
    , error/4
    , debug/4
]).

-include("common.hrl").

%% @doc 输出系统信息到控制台，带格式化参数，模块名和行号
info(Format, Args, Mod, Line) ->
    Msg = format("info", Format, Args, Mod, Line),
    io:format("~ts", [Msg]).

%% @doc 输出系统信息到控制台，带格式化参数，模块名和行号
error(Format, Args, Mod, Line) ->
    Msg = format("error", Format, Args, Mod, Line),
    io:format("~ts", [Msg]).

%% @doc 输出系统信息到控制台，带格式化参数，模块名和行号
debug(Format, Args, Mod, Line) ->
    Msg = format("debug", Format, Args, Mod, Line),
    io:format("~ts", [Msg]).


%% @doc 格式化日志信息
-spec format(string(), iodata(), list(), atom(), non_neg_integer()) -> iodata().
format(T, F, A, Mod, Line) ->
    {{Y, M, D}, {H, I, S}} = erlang:localtime(),
    Date = lists:concat([Y, "/", M, "/", D, " ", H, ":", I, ":", S]),
    case Line > 0 of
        false ->
            io_lib:format(lists:concat(["## ", T, " ~ts ", F, "~n"]), [Date] ++ A);
        true ->
            io_lib:format(lists:concat(["## ", T, " ~ts[~w:~w] ", F, "~n"]), [Date, Mod, Line] ++ A)
    end.
